import com.entity.User;
import org.junit.Test;

import java.time.LocalDate;
import java.util.Optional;

/**
 * @program: Demohander
 * ClassName: JDKOptional
 * @description: JDK1.8新特性之一：Optional类
 * @author: GuoTong
 * @create: 2021-06-07 16:24
 * @since JDK 1.8
 **/
@SuppressWarnings("all")
public class JDKOptional {
    /**
     * Description: Java 8 引入的一个很有趣的特性是 Optional  类。Optional 类主要解决的问题是臭名昭著的空指针异常
     *
     * @author: GuoTong
     * @date: 2021-06-07 17:26:05
     * @param:
     * @return:
     */

    private User user = new User().setName("原始Name").setId("原始号码").setBirthDay(LocalDate.now());

    @Test
    public void OptionalDemoByOne() {
        try {
            //本来user是未实例的东西
            user = Optional.ofNullable(this.user).
                    orElse(new User()).
                    setName("Optional").
                    setId("1008611").
                    setBirthDay(LocalDate.now().plusYears(-1));
            System.out.println(this.user);//User(birthDay=2020-06-07, name=Optional, id=1008611)
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * Description: isPresent();为了判断查询的类对象是否存在，采用此方法
     *
     * @author: GuoTong
     * @date: 2021-06-07 17:26:25
     * @param:
     * @return:
     */
    @Test
    public void OptionalDemoByTwo() {

        try {
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
